<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class ShopCouponsController extends CommonController {

    public function index()
    {
        $shop_id = I('shop_id');
        $map = [];
        if($shop_id){
            $map['shop_id'] = $shop_id;
        }
        list($list, $page) = $this->lists('ShopCoupons', $map, 'id asc');
        $shop_list = M('shop')->where(['status'=>1])->getField('id,shopname');
        $tpls = M('shop_coupon_tpls')->getField('id,img');
        if($list){
            foreach ($list as $key => &$row) {
                $row['shopname'] = isset($shop_list[$row['shop_id']]) ? $shop_list[$row['shop_id']] : '';
                $row['tpl_img'] = isset($tpls[$row['tpl_id']]) ? $tpls[$row['tpl_id']] : '';
                $row['condition_amount'] = $row['condition_amount']*1;
                $row['discount_amount'] = $row['discount_amount']*1;
                $row['get_start_time'] = date('Y-m-d', $row['get_start_time']);
                $row['get_end_time'] = date('Y-m-d', $row['get_end_time']);
            }
        }
        $this->assign('shop_list', $shop_list);
        $this->assign('shop_id', $shop_id);
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function users()
    {
        $coupon_id = I('coupon_id');
        $map = [];
        if($shop_id){
            $map['coupon_id'] = $coupon_id;
        }
        list($list, $page) = $this->lists('user_coupons', $map, 'id asc');
        $shop_list = M('shop')->where(['status'=>1])->getField('id,shopname');
        $tpls = M('shop_coupon_tpls')->getField('id,img');
        if($list){
            $user_ids = array_column($list, 'user_id');
            if($user_ids){
                $user_list = M('user')->where(['id'=>['in', $user_ids]])->getField('id,nickname');
            }else{
                $user_list = [];
            }
            foreach ($list as $key => &$row) {
                if($row['is_use']){
                    $status_desc = "已使用";
                }else{
                    if($now_time < $row['use_start_time']){
                        $status_desc = "未到可以使用时间";
                    }else if($now_time > $row['use_end_time']){
                        $status_desc = "过期";
                    }else{
                        $status_desc = "未使用";
                    }
                }
                $row['status_desc'] = $status_desc;
                $row['shopname'] = isset($shop_list[$row['shop_id']]) ? $shop_list[$row['shop_id']] : '';
                $row['nickname'] = isset($user_list[$row['user_id']]) ? $user_list[$row['user_id']] : '';
                $row['tpl_img'] = isset($tpls[$row['tpl_id']]) ? $tpls[$row['tpl_id']] : '';
                $row['condition_amount'] = $row['condition_amount']*1;
                $row['discount_amount'] = $row['discount_amount']*1;
                $row['use_start_time'] = date('Y-m-d', $row['use_start_time']);
                $row['use_end_time'] = date('Y-m-d', $row['use_end_time']);
                $row['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            }
        }
        $this->assign('shop_list', $shop_list);
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function show()
    {
        $id = I('id');
        $info = M('ShopCoupons')->where(['id'=>$id])->find();
        if(empty($info)){
            if(IS_POST){
                $this->ajaxReturn(array('s'=>false,'m'=>'信息不存在或已被删除!'));
            }
            $this->error('信息不存在或已被删除!');
        }
        if(IS_POST){
            $data = I('post.');

            $model = M('ShopCoupons');
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        
        $id = I('id');
        $res = M('ShopCoupons')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}